/* enhance $.getSctipt to handle mutiple scripts */
var getScript = jQuery.getScript;
jQuery.getScript = function( resources, callback ) {
	
	/* if first argument not array of resources */
	(typeof resources === 'string') && (resources = [ resources ]);	

    $.when.apply( null, $.map(resources, getScript) ).then(function() {
		callback && callback();
    });
};

/* Create $.getStyle to handle mutiple stylesheets */
jQuery.getStyle = function( resources, callback ) {
    
    var currentNum = document.styleSheets.length;
	
	resources.map(function(href) {
		var link = document.createElement("link");
		link.type = "text/css", link.rel = "stylesheet", link.href = href;
		document.getElementsByTagName("head")[0].appendChild(link); 
	});	
	
	var timer = setInterval(function() {
    if (document.styleSheets.length == currentNum + resources.length) {
		callback && callback(); clearInterval(timer);
    }}, 10);
    
}

function syntaxHighlighter() {
	
	var base_url = 'http://alexgorbatchev.com/pub/sh/current/scripts/';
	var scripts = ['JScript.js','Plain.js','Bash.js','Sql.js','Perl.js','Php.js','Cpp.js','Xml.js'];
	
	this.init = function() {
		// add base url && pattern
		scripts = scripts.map(function(x) { 
			return x.replace(/^/g,'shBrush').replace(/^/g,base_url);			
		});
		// load core //
		$.getScript(base_url + 'shCore.js', this.highlight );
		this.status = true;
	}
	
	this.highlight = function() {		
		$.getScript(scripts, function() {
			SyntaxHighlighter.config.bloggerMode = true;  
			SyntaxHighlighter.defaults['toolbar'] = false;			
			$.getStyle([
				'http://alexgorbatchev.com/pub/sh/current/styles/shCore.css',
				'https://sites.google.com/site/svtpolevik/shThemeBlogger.css'
			], function() {
				SyntaxHighlighter.highlight();
			});
		});	
	}
}




function gmap_render(){	
	$("#map").gMap({ 
		markers: [{ 
			latitude: 50.515500, longitude: 30.61970,
			html: "Тут есть Булик =)",
			popup: false 
		}],	
		latitude: 50.515500,
		longitude: 30.61970,
		zoom: 17 
	}); 
}

function graph_rendering() {
	var count = 0;
	var margin_cnt = 30;
	var fix_height = parseFloat($("#skill-rating .graph").css("height"));

	$("td.values label span").each(function(){
			
		count++;
		color = $(this).css("backgroundColor");
		grph_el_id = "#skill-rating_bar_" + count;
	
		height_el = fix_height * $(this).text() / 100;
		margin_el = "-" + (height_el + 1) + "px";
		
		$(grph_el_id).css({backgroundColor: color, marginLeft: margin_cnt});		
		$(grph_el_id).animate({height: height_el, marginTop: margin_el }, margin_cnt * 5);
			
		$(this).parent().animate({opacity: 1},margin_cnt * 5);
			
		margin_cnt = margin_cnt + 60;
		
	});
}


$(function() {

var highlight = new syntaxHighlighter();

var api_url = "http://svt-polevik.appspot.com/vkapi?callback=?";

$.getJSON(api_url,{method: 'users.get', uid : 155135124, fields : 'bdate,city,country,connections,online,about' }, function(data) {
		
		//get city //
		
		main_result = data.response[0];
		console.log(JSON.stringify(main_result));
		
		$.getJSON(api_url,{method: 'places.getCountryById', cids : main_result.country }, function(data) {
			main_result.country = data.response[0].name;
			$.getJSON(api_url,{method: 'places.getCityById', cids : main_result.city }, function(data) {
				main_result.city = data.response[0].name;
				$.get('http://svt-polevik.appspot.com/templates/vk_profile.html', function(template) {
					$("#profile").html($.tmpl(template, main_result))
					$("#profile > div").fadeIn();
				});
			});
		});
		
	
	}
);



	

	$("nav.menu li:last-child a").click(function() {
		$(".menu-vertical").show();		
		$('input', this).focus().focusout(function() {
			$(".menu-vertical").hide();
		});	
		return false;
	});	
	

	$("nav.menu li:not(:last-child) a, nav.menu-vertical li:not(:first-child) a").mousedown(function() {
	
		tab_name = $(this).attr("href").substr(1);
		
		$("nav li.active").removeClass("active");
		$(this).parent().addClass("active");
		
		$('section').hide();
		$('section#' + tab_name).show();
		
		switch (tab_name) {
			case 'resume':
				graph_rendering();
				break
			case 'contact':
				gmap_render();		
				break
			case 'blog':				
				highlight.status || highlight.init();
				break
			default:
				break
		}
		
		if($(this).parents("nav").attr("class") == "menu") return false;		
		$("nav.menu li:last-child").addClass("active");
		
	}).click(function() { return false; });

	
});
